package com.zycfc.zsf.boot.util.io;

import java.util.zip.*;
import java.net.*;
import java.nio.charset.*;
import java.util.*;
import java.io.*;

public abstract class IOUtils
{
    private static final int DEFAULT_BUFFER_SIZE = 4096;
    private static final int EOF = -1;
    
    public static void closeQuietly(final Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            }
            catch (IOException ex) {}
        }
    }
    
    public static void closeQuietly(final ZipFile zipfile) {
        if (zipfile != null) {
            try {
                zipfile.close();
            }
            catch (IOException ex) {}
        }
    }
    
    public static void closeQuietly(final Socket socket) {
        if (socket != null) {
            try {
                socket.close();
            }
            catch (IOException ex) {}
        }
    }
    
    public static String toString(final InputStream input) throws IOException {
        final InputStreamReader reader = new InputStreamReader(input, StandardCharsets.UTF_8);
        return toString(reader);
    }
    
    public static String toString(final Reader input) throws IOException {
        final BufferedReader reader = toBufferedReader(input);
        final StringWriter sw = new StringWriter();
        copy(reader, sw);
        return sw.toString();
    }
    
    public static List<String> toLines(final InputStream input) throws IOException {
        return toLines(new InputStreamReader(input, StandardCharsets.UTF_8));
    }
    
    public static List<String> toLines(final Reader input) throws IOException {
        final BufferedReader reader = toBufferedReader(input);
        final List<String> list = new ArrayList<String>();
        for (String line = reader.readLine(); line != null; line = reader.readLine()) {
            list.add(line);
        }
        return list;
    }
    
    public static void write(final String data, final OutputStream output) throws IOException {
        if (data != null) {
            output.write(data.getBytes(StandardCharsets.UTF_8));
        }
    }
    
    public static void write(final String data, final Writer output) throws IOException {
        if (data != null) {
            output.write(data);
        }
    }
    
    public static long copy(final Reader input, final Writer output) throws IOException {
        final char[] buffer = new char[4096];
        long count = 0L;
        int n;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }
    
    public static long copy(final InputStream input, final OutputStream output) throws IOException {
        final byte[] buffer = new byte[4096];
        long count = 0L;
        int n;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }
    
    public static BufferedReader toBufferedReader(final Reader reader) {
        return (BufferedReader)((reader instanceof BufferedReader) ? reader : new BufferedReader(reader));
    }
    
    public static void write(final String filePath, final byte[] data) throws IOException {
        final OutputStream outputStream = new FileOutputStream(filePath);
        outputStream.write(data);
        outputStream.flush();
        outputStream.close();
    }
}
